from typing import Optional

from fastapi import HTTPException

from pydantic import BaseModel, field_validator


class Region_schema(BaseModel):
    id: Optional[int] = None
    name: str
    parentRegion: str
    regionType: int
    latitude_1: Optional[float]
    longitude_1: Optional[float]
    latitude_2: Optional[float]
    longitude_2: Optional[float]
    poi: bool


class Region_schema_with_accountId(Region_schema):
    accountId: int


class Upload_region_form(BaseModel):
    name: str
    parentRegion: str
    regionType: int
    latitude_1: Optional[float]
    longitude_1: Optional[float]
    latitude_2: Optional[float]
    longitude_2: Optional[float]
    poi: bool

    @field_validator('latitude_1')
    @classmethod
    def validate_latitude_1(cls, coord: Optional[float]):
        if coord is None:
            raise HTTPException(status_code=400, detail="bad latitude")
        return coord

    @field_validator('longitude_1')
    @classmethod
    def validate_longitude_1(cls, coord: Optional[float]):
        if coord is None:
            raise HTTPException(status_code=400, detail="bad longitude")
        return coord

    @field_validator('latitude_2')
    @classmethod
    def validate_latitude_2(cls, coord: Optional[float]):
        if coord is None:
            raise HTTPException(status_code=400, detail="bad latitude")
        return coord

    @field_validator('longitude_2')
    @classmethod
    def validate_longitude_2(cls, coord: Optional[float]):
        if coord is None:
            raise HTTPException(status_code=400, detail="bad longitude")
        return coord


class Full_regionType_schema(BaseModel):
    id: int
    type: Optional[str]


class Upload_regionType_form(BaseModel):
    type: Optional[str]

    @field_validator('type')
    @classmethod
    def validate_type(cls, type: Optional[str]):
        if type is None:
            return None
        type = type.replace(" ", "")
        if not bool(type):
            raise HTTPException(status_code=400, detail="bad type")
        return type


class Output_edit_region_schema(BaseModel):
    id: Optional[int] = None
    name: str
    parentRegion: str
    latitude_1: Optional[float]
    longitude_1: Optional[float]
    latitude_2: Optional[float]
    longitude_2: Optional[float]
